package com.resource.animation
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	public class AnimationDecoder
	{
		private static const posList:Object = {"hpBar":true, "name":true, "position":true, "ride":true}; // 只要第一帧左上点位置信息
		private static const rectList:Object = {"hitRect":true, "attRect":true, "sendBullet":true}; // 所有帧的矩形信息
		
		public function AnimationDecoder()
		{
		}
		
		public static function decode(bytes:ByteArray):Object{
			if(bytes){
				var data:Object = bytes.readObject();
				
				var frameList:Array = [];
				var arr:Array = data.frameList as Array;
				for(var i:int=0; i<arr.length; i++){
					var obj:Object = {};
					obj.ptr = arr[i].ptr;
					obj.xoff = arr[i].xoff;
					obj.yoff = arr[i].yoff;
					var dt:ByteArray = arr[i].data as ByteArray;
					if(dt){
						dt.uncompress();
						var bmd:BitmapData = new BitmapData(dt.readUnsignedInt(), dt.readUnsignedInt(), true, 0);
						bmd.setPixels(bmd.rect, dt);
						obj.data = bmd;
					}else{
						obj.data = null;
					}
					frameList.push(obj);
				}
				var res:Object = {};
				res.frameList = frameList;
				
				for(var k:* in posList){
					if(data[k]){
						var point:Point = new Point(data[k].x, data[k].y);
						res[k] = point;
					}
				}
				
				for(var k:* in rectList){
					if(data[k]){
						var frameList:Array = [];
						var arr:Array = data[k] as Array;
						for(var i:int=0; i<arr.length; i++){
							var obj:Object = {};
							obj.x1 = arr[i].x1;
							obj.y1 = arr[i].y1;
							obj.x2 = arr[i].x2;
							obj.y2 = arr[i].y2;
							frameList.push(obj);
						}
						res[k] = frameList;
					}
				}
				
				return res;
			}
			return null;
		}
	}
}